package com.xbongbong.rollback.service.impl.update;

import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.pro.domain.entity.ReRollBackEntity;
import com.xbongbong.pro.service.toolbox.elasticsearch.help.ProEsHelper;
import com.xbongbong.pro.thread.ThreadPoolBeanConfig;
import com.xbongbong.rollback.factory.FormDataUpdateRollBackFactory;
import com.xbongbong.rollback.service.AbstractFormDataUpdateRollBackService;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * @author 吴峰
 * @date 2021/03/11 15:44
 */
@Service("invoiceUpdateRollBackServiceImpl")
public class InvoiceUpdateRollBackServiceImpl extends AbstractFormDataUpdateRollBackService {

    private static final Logger LOG = LoggerFactory.getLogger(InvoiceUpdateRollBackServiceImpl.class);

    @Resource
    private ProEsHelper proEsHelper;
    @Resource
    private ThreadPoolBeanConfig threadPoolBeanConfig;

    @PostConstruct
    public void register() {
        FormDataUpdateRollBackFactory.FORM_DATA_UPDATE_ROLL_BACK_SERVICE_MAP.put(String.valueOf(XbbRefTypeEnum.INVOICE.getCode()), this);
    }


    @Override
    public void rollBack4Update(PaasFormDataEntity newPaasFormDataEntity, PaasFormDataEntity oldPaasFormData, Long dataId,
                                String corpid, Integer saasMark, Integer businessType, ReRollBackEntity reRollBackEntity) {
        Runnable runnable = () -> {
            try {
                proEsHelper.otherRefEsDataRollBack(newPaasFormDataEntity,corpid, String.valueOf(businessType),saasMark, reRollBackEntity);
            } catch (Exception e2) {
                LOG.error("回滚其他表数据失败", e2);
            }
        };
        threadPoolBeanConfig.addUpdateDelThreadPool().execute(runnable);

    }
}
